package com.example.demo.controller;

import com.example.demo.bean.Student;
import com.example.demo.bean.User;
import com.example.demo.dao.IStudentDao;
import com.example.demo.serivce.StudentService;
import com.example.demo.serivce.UserService;
import com.example.demo.serivce.impl.StudentServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * @author zzy
 */
@Controller
@RequestMapping("/")
public class LoginController {


    @Autowired
    private UserService userService;

    @RequestMapping(value = "/two",method = {RequestMethod.POST,RequestMethod.GET})

    public String two(Model model, @RequestParam("username") String username,@RequestParam("password") String password) {

        System.out.println(username+"?"+password);

        List<User> userList = userService.selectUser(null);

        Subject subject = SecurityUtils.getSubject();

        UsernamePasswordToken token = new UsernamePasswordToken(username,password);

        try {

            subject.login(token);
            //建立该对象会执行认证逻辑
            model.addAttribute("userList",userList);
            return "hello";
        } catch (AuthenticationException e) {

            model.addAttribute("msg","账号或密码错误，请重试!");

            return "index";
        }
    }
    @RequestMapping("/login")
    public String toLogin() {
        return "index";
    }
}
